package org.scboy.dbms.service.imp;

import java.util.List;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.dbms.dao.InterFrontDao;
import org.scboy.dbms.dao.InterLangDao;
import org.scboy.dbms.service.InterFrontService;
import org.scboy.utils.entitys.coobi.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class InterFrontServiceImp implements InterFrontService {

	@Autowired
	EntityFactory factory;
	
	@Override
	public Model get(String lang) {
		InterFrontDao dao=factory.getDao(InterFrontDao.class);
		Model data= dao.get(Model.create().add("lang", lang));
		if(data==null) {
			data=Model.create().add("lang", lang).add("langConfig", "export default {}");
		}
		return data;
	}

	@Override
	public boolean update(Model model) {
		InterFrontDao dao=factory.getDao(InterFrontDao.class);
		int count=0;
		if(dao.count(Model.create().add("lang", model.getString("lang")))>0) {
			count=dao.update(model);
		}else {
			count=dao.insert(model);
		}
		if(count>0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public List<Model> list() {
		InterFrontDao dao=factory.getDao(InterFrontDao.class);
		InterLangDao langDao=factory.getDao(InterLangDao.class);
		List<Model> list=langDao.select(Model.create());
		for (Model model : list) {
			Model config=dao.get(Model.create().add("lang", model.getString("lang")));
			if(config==null) {
				model.add("config", "export default {}");
			}else {
				model.add("config", config.getString("langConfig"));
			}
		}
		return list;
	}

	@Override
	public String lang(String lang) {
		InterFrontDao dao=factory.getDao(InterFrontDao.class);
		Model data= dao.get(Model.create().add("lang", lang));
		String config="export default {}";
		if(data!=null) {
			config=data.getString("langConfig");
		}
		return config;
	}
}
